package com.smart.campus.school.service.impl;

import java.util.List;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.domain.ExamRoomClasses;
import com.smart.campus.school.mapper.ExamRoomClassesMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ExamRoomScheduleMapper;
import com.smart.campus.school.domain.ExamRoomSchedule;
import com.smart.campus.school.service.IExamRoomScheduleService;
import com.smart.campus.common.core.text.Convert;

/**
 * 考场安排Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-06-03
 */
@Service
public class ExamRoomScheduleServiceImpl implements IExamRoomScheduleService 
{
    @Autowired
    private ExamRoomScheduleMapper examRoomScheduleMapper;
    @Autowired
    private ExamRoomClassesMapper examRoomClassesMapper;

    /**
     * 查询考场安排
     * 
     * @param id 考场安排ID
     * @return 考场安排
     */
    @Override
    public ExamRoomSchedule selectExamRoomScheduleById(Long id)
    {
        ExamRoomSchedule schedule = examRoomScheduleMapper.selectExamRoomScheduleById(id);
        ExamRoomClasses roomClasses = new ExamRoomClasses();
        roomClasses.setScheduleId(id);
        schedule.setList(examRoomClassesMapper.selectExamRoomClassesList(roomClasses));
        return schedule;
    }

    /**
     * 查询考场安排列表
     * 
     * @param examRoomSchedule 考场安排
     * @return 考场安排
     */
    @Override
    public List<ExamRoomSchedule> selectExamRoomScheduleList(ExamRoomSchedule examRoomSchedule)
    {
        return examRoomScheduleMapper.selectExamRoomScheduleList(examRoomSchedule);
    }

    /**
     * 新增考场安排
     * 
     * @param examRoomSchedule 考场安排
     * @return 结果
     */
    @Override
    public int insertExamRoomSchedule(ExamRoomSchedule examRoomSchedule)
    {
        examRoomSchedule.setCreateTime(DateUtils.getNowDate());
        int id = examRoomScheduleMapper.insertExamRoomSchedule(examRoomSchedule);
        if(CollectionUtils.isNotEmpty(examRoomSchedule.getList())){
            for (ExamRoomClasses roomClazz : examRoomSchedule.getList()) {
                roomClazz.setExamId(examRoomSchedule.getExamId());
                roomClazz.setCreateBy(examRoomSchedule.getCreateBy());
                roomClazz.setCreateTime(examRoomSchedule.getCreateTime());
                roomClazz.setScheduleId(examRoomSchedule.getId());
                examRoomClassesMapper.insertExamRoomClasses(roomClazz);
            }
        }
        return id;
    }

    /**
     * 修改考场安排
     * 
     * @param examRoomSchedule 考场安排
     * @return 结果
     */
    @Override
    public int updateExamRoomSchedule(ExamRoomSchedule examRoomSchedule)
    {
        examRoomSchedule.setUpdateTime(DateUtils.getNowDate());
        if(CollectionUtils.isNotEmpty(examRoomSchedule.getList())){
            for (ExamRoomClasses roomClazz : examRoomSchedule.getList()) {
                if(roomClazz.getId()==null&&roomClazz.getRoomId()!=null&&roomClazz.getLocation()!=null){
                    roomClazz.setExamId(examRoomSchedule.getExamId());
                    roomClazz.setCreateBy(examRoomSchedule.getCreateBy());
                    roomClazz.setCreateTime(examRoomSchedule.getCreateTime());
                    roomClazz.setScheduleId(examRoomSchedule.getId());
                    examRoomClassesMapper.insertExamRoomClasses(roomClazz);
                }
            }
        }
        return examRoomScheduleMapper.updateExamRoomSchedule(examRoomSchedule);
    }

    /**
     * 删除考场安排对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExamRoomScheduleByIds(String ids)
    {
        return examRoomScheduleMapper.deleteExamRoomScheduleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除考场安排信息
     * 
     * @param id 考场安排ID
     * @return 结果
     */
    @Override
    public int deleteExamRoomScheduleById(Long id)
    {
        return examRoomScheduleMapper.deleteExamRoomScheduleById(id);
    }
}
